#include "InetAddress.h"

using std::cout;
using std::endl;

InetAddress::InetAddress(const std::string &ip, unsigned short port)
{
    memset(&_addr, 0, sizeof(_addr));
    _addr.sin_port = htons(port);
    _addr.sin_family = AF_INET;
    _addr.sin_addr.s_addr = inet_addr(ip.c_str());
}

InetAddress::InetAddress(const sockaddr_in &addr)
{
    _addr.sin_addr.s_addr = addr.sin_addr.s_addr;
    _addr.sin_family = addr.sin_family;
    _addr.sin_port = addr.sin_port;
}

const std::string InetAddress::ip() const
{
    return inet_ntoa(_addr.sin_addr);
}

unsigned short InetAddress::port() const
{
    return ntohs(_addr.sin_port);
}

sockaddr_in InetAddress::getInetAddress() const
{
    return _addr;
}